package com.apestech.formwork.stream;

import com.apestech.framework.file.callback.WriteBufferFileCallBackHandler;
import com.apestech.framework.stream.ArrayObject;

public abstract class ArrayObjectWriteFile implements WriteBufferFileCallBackHandler{
	
	protected abstract String write(ArrayObject obj, int index) throws Exception ;
	
	private final ArrayObject obj;
	private int pos=0;
	private int maxCount=-1;
	private boolean maxflag=false;
	
	public ArrayObjectWriteFile(ArrayObject obj, int pos){
		this.obj=obj;
		this.pos=pos;
	}
	
	public ArrayObjectWriteFile(ArrayObject obj, int pos, int maxCount){
		this.obj=obj;
		this.pos=pos;
		this.maxCount=maxCount;
	}

	public boolean canWrite() throws Exception {
		if(this.maxflag) return false;
		return this.obj.next();
	}

	public String write(int index) throws Exception {
		if(this.maxCount==index-1 && this.maxCount>-1){
			this.maxflag=true;
		}
		return write(this.obj, this.obj.getIndex()+this.pos);
	}

}
